package com.dm.service.sms;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.dm.config.SystemConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Set;

@Service
public class SmsServiceImpl implements SmsService {

    //传架系统配置类对象
    @Resource
    private SystemConfig systemConfig;



    /*@Override
    public Dto sendSms(String userCode) throws Exception {
        HashMap<String, Object> parm = new HashMap<String, Object>();

        parm.put("userCode",userCode);
        List<ItripUser> itripUserList=itripUserMapper.getItripUserListByMap(parm);
        if(EmptyUtils.isEmpty(itripUserList)) {


            restAPI.init(systemConfig.getSmsServerIP(), systemConfig.getSmsServerPort());
            // 初始化服务器地址和端口，生产环境配置成app.cloopen.com，端口是8883.
            restAPI.setAccount(systemConfig.getSmsAccountSid(), systemConfig.getSmsAuthToken());
            // 初始化主账号名称和主账号令牌，登陆云通讯网站后，可在控制首页中看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN。
            restAPI.setAppId(systemConfig.getSmsAppID());
            // 请使用管理控制台中已创建应用的APPID。
            *//*生成4为号码*//*
            Integer rand = MD5.getRandomCode();
            //保存到redis数据库中
            redisAPI.set(Constants.SMS_PIX + userCode, rand.toString());

            result = restAPI.sendTemplateSMS(userCode, "1", new String[]{rand.toString(), "1"});
            System.out.println("SDKTestGetSubAccounts result=" + result);
            if ("000000".equals(result.get("statusCode"))) {
                //正常返回输出data包体信息（map）
                HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
                Set<String> keySet = data.keySet();
                for (String key : keySet) {
                    Object object = data.get(key);
                    System.out.println(key + " = " + object);
                }
            } else {
                redisAPI.delete(Constants.SMS_PIX + userCode);
                //异常返回输出错误码和错误信息
                System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
                return  DtoUtil.returnSuccess("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            }
        }else{
            return  DtoUtil.returnSuccess("用户已注册！");
        }
        return DtoUtil.returnSuccess("注册成功！");
    }
*/


    /**
     * 发送短信
     *
     * @param to
     * @param templateId
     * @param datas
     * @throws Exception
     */
    @Override
    public void sendSms(String to, String templateId, String[] datas) throws Exception {
        //创建手机短信对象
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
        restAPI.init(systemConfig.getSmsServerIP(), systemConfig.getSmsServerPort());
        // 初始化服务器地址和端口，生产环境配置成app.cloopen.com，端口是8883.
        restAPI.setAccount(systemConfig.getSmsAccountSid(), systemConfig.getSmsAuthToken());
        // 初始化主账号名称和主账号令牌，登陆云通讯网站后，可在控制首页中看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN。
        restAPI.setAppId(systemConfig.getSmsAppID());
        HashMap<String, Object> result = null;
        //发送
        result = restAPI.sendTemplateSMS(to, templateId, datas);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
        } else {
            throw new Exception(result.get("statusCode").toString() + ":" + result.get("statusmMsg").toString());
        }

    }


}


